/*
 * Copyright (c) 2019 marmot-client
 * marmot-client is licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 *    http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 */

package com.skm.open.sensitive.word.processor.autoconfigure;


import com.skm.open.sensitive.word.processor.SensitiveWordProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author skqing
 * @version 1.0.0
 * @copyright(C) 思酷曼
 */

@Configuration
@EnableConfigurationProperties(SensitiveWordProcessorProperties.class)
//@ConditionalOnClass({ MQAdmin.class, ObjectMapper.class })
/**缺少该property时是否可以加载。如果为true，没有该property也会正常加载；反之报错。这里设置为true是为了用户依赖了此JAR但没配置这个属性时仍然能够启动服务*/
@ConditionalOnProperty(prefix = Const.PROPERTIES_PREFIX, value = "filePath", matchIfMissing = true)
//@Import({ JacksonFallbackConfiguration.class, ListenerContainerConfiguration.class, ExtProducerResetConfiguration.class })
//@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class SensitiveWordProcessorAutoConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(SensitiveWordProcessorAutoConfiguration.class);

//    @Autowired
//    private Environment environment;

//    @PostConstruct
//    public void checkProperties() {
//        String filePath = environment.getProperty(Const.PROPERTIES_PREFIX+".filePath", String.class);
//        LOGGER.debug("{}.filePath = {}", Const.PROPERTIES_PREFIX, filePath);
//        if (filePath == null) {
//            LOGGER.warn("The spring property '{}.filePath' is not defined, use the default file name sensitive-word.properties", Const.PROPERTIES_PREFIX);
//            filePath = "sensitive-word.properties";
//        }
//        if ( !(filePath.endsWith(".properties")) ) {
//            LOGGER.warn("The necessary spring property '{}.filePath' is not illegal, all SnifferDog beans creation are skipped!", Const.PROPERTIES_PREFIX);
//        }
//    }

    @Bean
    public SensitiveWordProcessor sensitiveWordProcessorWrapper(SensitiveWordProcessorProperties sensitiveWordProcessorProperties) {
        SensitiveWordProcessor sensitiveWordProcessor = new SensitiveWordProcessor(sensitiveWordProcessorProperties);
        LOGGER.info("-----SensitiveWordProcessor is init-----");
        return sensitiveWordProcessor;
    }
}
